//
//  PlayerCollectionViewController.m
//  ReviewDemo
//
//  Created by 龙 on 2018/5/9.
//  Copyright © 2018年 long. All rights reserved.
//

#define CellIdentifer @"AVPlayerCollectionViewCell"

#import "PlayerCollectionViewController.h"
#import "AVPlayerCollectionViewCell.h"
#import "AVPlayerTools.h"
#import <Masonry.h>
@interface PlayerCollectionViewController ()
<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) NSMutableArray <NSString *> *dataArray;
@end

@implementation PlayerCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.collectionView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDelegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"将显示 ----- %@",[NSString stringWithFormat:@"第 %ld 行",indexPath.row]);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AVPlayerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifer forIndexPath:indexPath];
    NSLog(@"已显示 ----- %@",[NSString stringWithFormat:@"第 %ld 行",indexPath.row]);
    
    NSString *videoString = self.dataArray[indexPath.item];
    NSURL *videoURL = [NSURL URLWithString:videoString];
    AVPlayerCollectionViewCell *playerCell = (AVPlayerCollectionViewCell *)cell;
    [[AVPlayerTools sharedInstance] playVideoUrl:videoURL coverImageurl:@"" showInSuperView:playerCell.videoView];
    playerCell.adressLable.text = [NSString stringWithFormat:@"第 %ld 行",indexPath.row];
    
    NSLog(@"cell ----- %@",cell);
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"消失 ----- %@",[NSString stringWithFormat:@"第 %ld 行",indexPath.row]);

}


#pragma mark - setter and getter

- (NSMutableArray<NSString *> *)dataArray
{
    if (_dataArray == nil)
    {
        NSString *videoString = @"http://img.multshows.com/test/testViedo/1967088684R919F";
        _dataArray = [NSMutableArray array];
        
        for (int i = 0; i < 12; i ++)
        {
            [_dataArray addObject:videoString];
        }
    }
    return _dataArray;
}

- (UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(ScreenWidth - 20, ScreenWidth);
                
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.delegate = self;
        [collectionView registerClass:AVPlayerCollectionViewCell.class forCellWithReuseIdentifier:CellIdentifer];
        _collectionView = collectionView;
    }
    return _collectionView;
}

@end
